*----------------------------------------------------------------------------------------------------------	* 
* RESEARCHERS:		EARN												   									*
* PROGRAMMED BY:	Linea Hasager																			*
* DESCRIPTION:		Balancing tests of covariates and tests of placement patterns changing over time. 		*
* CREATED:			Mar. 14, 2018																		   	*
* LAST MODIFIED:	May 27, 2022													       					*
*---------------------------------------------------------------------------------------------------------	*

clear 
set more off

*DIRECTORIES
global in "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Input"
global output "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Output"
global temp "L:\Workdata\707455\Papers\ABFHP_1999reform\Code\ReStat\Temp"



*--------------------------------------------------------------------------------------------------------	

/* LOAD DATA*/
use "${in}\Analysis_sample.dta", replace




*SET X-AXIS VALUES
global x_time -24 -12 0 12 24



*ASYLUM PROCESSING TIME
foreach var in waittime {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(200)800, format(%10.0fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Days"))
graph export "${output}\oa_figure1c.png", replace width(5000)
drop antald _min _max w_n
}


*AGE
foreach var in age {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(25(5)40, format(%10.0fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Years"))
graph export "${output}\oa_figure2a.png", replace width(5000)
drop antald _min _max w_n
}


*MARITAL STATUS
foreach var in married  {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2b.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}

*GENDER
foreach var in female {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2c.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}


*CHILDREN 0-2Y
foreach var in kids02 {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.5)1.5, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Children"))
graph export "${output}\oa_figure2d.png", replace width(5000)
drop antald _min _max w_n
}



*CHILDREN 3-17Y
foreach var in kids018 {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.5)1.5, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Children"))
graph export "${output}\oa_figure2e.png", replace width(5000)
drop antald _min _max w_n
}

*ORIGIN COUNTRY, IRAQ
foreach var in iraq  {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2f.png", replace width(5000)
drop antald _min _max w_n
}

*ORIGIN COUNTRY, AFGHANISTAN
foreach var in afghanistan {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2g.png", replace width(5000)
drop antald _min _max w_n
}


*ORIGIN COUNTRY, OTHER
foreach var in other_country {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2h.png", replace width(5000)
drop antald _min _max w_n
}



*LANGUAGE, ARABIC
foreach var in arabic  {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2i.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}


*LANGUAGE, DARI
foreach var in dari  {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2j.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}

*DANISH LANGUAGE TRACK, 1
foreach var in danishf1 {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2k.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}

*DANISH LANGUAGE TRACK, 2
foreach var in danishf2  {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2l.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}

*DANISH LANGUAGE TRACK, 3
foreach var in danishf3  {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure2m.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}



*REFUGEE PERMIT TYPE, QUOTA
foreach var in quota {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2n.png", replace width(5000)
drop antald _min _max w_n
}


*REFUGEE PERMIT TYPE, CONVENTION
foreach var in convention {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2o.png", replace width(5000)
drop antald _min _max w_n
}


*REFUGEE PERMIT TYPE, FAMILY-REUNIFIED
foreach var in familyref {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2p.png", replace width(5000)
drop antald _min _max w_n
}


*REFUGEE PERMIT TYPE, OTHER
foreach var in other_permit {
bysort m_nr: egen antald=count(pnr) if `var' ==1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max/2
local min=-_min/2
rdplot antald m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options(ylab(0(50)250, format(%10.0fc))  xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Number of Refugees"))
graph export "${output}\oa_figure2q.png", replace width(5000)
drop antald _min _max w_n
}




*URBAN MUNICIPALITY
foreach var in urban {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure3a.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}


*PLACED IN FIVE LARGEST CITIES
foreach var in top5 {
bysort m_nr: egen antald=count(pnr) if `var' !=.
bysort m_nr: egen group1=sum(`var') 
gen group0=antald-group1
gen w_n=mofd(tilladelsesdato)-468 if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0)
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4 & (group1 > 4 | group1==0) & (group0>4 | group0==0), nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(0.2)1, format(%10.1fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure3b.png", replace width(5000)
drop antald group0 group1 _min _max w_n
}



*EMPLOYMENT RATE IN MUNICIPALITY
foreach var in emprate1996 {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0.7(0.05).8, format(%10.2fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure3c.png", replace width(5000)
drop antald _min _max w_n
}


*UNEMPLOYMENT RATE IN MUNICIPALITY
foreach var in urate1996  {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0.07(0.02).11, format(%10.2fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure3d.png", replace width(5000)
drop antald _min _max w_n
}


*AVERAGE EARNINGS IN MUNICIPALITY
foreach var in avg_loenmv_131996 {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(25(2)31, format(%10.0fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Thousand USD"))
graph export "${output}\oa_figure3e.png", replace width(5000)
drop antald _min _max w_n
}


*NUMBER OF CO-NATIONALS
foreach var in n_conationals {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0(200)800, format(%10.0fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle(""))
graph export "${output}\oa_figure3f.png", replace width(5000)
drop antald _min _max w_n
}



*SHARE OF CO-NATIONALS
foreach var in s_conationals {
bysort month_nr: egen antald=count(pnr) if `var' !=.
gen w_n=mofd(tilladelsesdato)-468 if antald > 4
egen _max=max(w_n)
egen _min=min(w_n)
local max=_max
local min=-_min
rdplot `var' m_nr if antald > 4, nbins(`min' `max') kernel(triangular) c(0) p(1) graph_options( ylab(0.0(0.0025).005, format(%10.4fc)) xtitle("Month of Admission") tlabel(${x_time}) graphregion(fcolor(white) margin(medlarge)) legend(off) ytitle("Share"))
graph export "${output}\oa_figure3g.png", replace width(5000)
drop antald _min _max w_n
}











*----------------------------------------------------------------------------------------------------------	*
* CALCULATE MEAN AND STANDARD DEVIATION OF INDIVIDUAL COVARIATES
*----------------------------------------------------------------------------------------------------------	*


preserve
egen mean=mean(danishf3)
egen sd=sd(danishf3)
keep mean sd
keep if _n==1
gen variable="danishf3"
save "${temp}\covar.dta", replace
restore

foreach var in danishf2 danishf1 iraq familyref other_country afghanistan kids02 kids018 married female age quota convention other_permit waittime arabic dari {
preserve
egen mean=mean(`var')
egen sd=sd(`var')
keep mean sd
keep if _n==1
gen variable="`var'"
append using "${temp}\covar.dta"
save "${temp}\covar.dta", replace
restore
}


*----------------------------------------------------------------------------------------------------------	*
* CALCULATE MEAN AND STANDARD DEVIATION OF MUNICIPALITY CHARACTERISTICS
*----------------------------------------------------------------------------------------------------------	*
preserve
egen mean=mean(emprate1996)
egen sd=sd(emprate1996)
keep mean sd
keep if _n==1
gen variable="emprate1996"
save "${temp}\covar_mun.dta", replace
restore

foreach var in urate1996 avg_loenmv_131996 urban top5 n_conationals s_conationals {
preserve
egen mean=mean(`var')
egen sd=sd(`var')
keep mean sd
keep if _n==1
gen variable="`var'"
append using "${temp}\covar_mun.dta"
save "${temp}\covar_mun.dta", replace
restore
}


*1997-2000
preserve
keep if year_admission==1997 | year_admission==2000
egen mean=mean(emprate1996)
egen sd=sd(emprate1996)
keep mean sd
keep if _n==1
gen variable="emprate1996"
save "${temp}\covar_mun1997_2000.dta", replace
restore

foreach var in  urate1996 avg_loenmv_131996 urban top5  n_conationals s_conationals  {
preserve
keep if year_admission==1997 | year_admission==2000
egen mean=mean(`var')
egen sd=sd(`var')
keep mean sd
keep if _n==1
gen variable="`var'"
append using "${temp}\covar_mun1997_2000.dta"
save "${temp}\covar_mun1997_2000.dta", replace
restore
}



*----------------------------------------------------------------------------------------------------------	*
	

*RDD ESTIMATES: BALANCING TESTS

*----------------------------------------------------------------------------------------------------------	*


set more off
foreach var in female {
preserve
rdrobust `var' day_nr,  kernel(tri) c(0) p(1) 
eststo `var'_e
regsave RD_Estimate using "${temp}\RD.dta", replace ci addlabel(variable,`var') 
restore
}

foreach var in married familyref iraq afghanistan other_country kids02 kids018  quota convention other_permit waittime age danishf1 danishf2 danishf3 arabic dari {
preserve
rdrobust `var' day_nr,  kernel(tri) c(0) p(1)
eststo `var'_e
regsave RD_Estimate using "${temp}\RD.dta", append ci addlabel(variable, `var')
restore
}



set more off
foreach var in emprate1996 {
preserve
rdrobust `var' day_nr,  kernel(tri) c(0) p(1) 
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_mun.dta", replace ci addlabel(variable,`var') 
restore
}

foreach var in  urate1996  avg_loenmv_131996  urban top5 n_conationals s_conationals {
preserve
rdrobust `var' day_nr,  kernel(tri) c(0) p(1)
eststo `var'_e
regsave RD_Estimate using "${temp}\RD_mun.dta", append ci addlabel(variable, `var')
restore
}


*TEST WHETHER REFORM CHANGED PLACEMENT PATTERNS LATER THAN EXACT TIMING OF REFORM:
*COMPARE MUNICIPALITY DIFFERENCES FOR 1997 AND 2000 ADMISSIONS
foreach var in emprate1996 {
preserve
rdrobust `var' day_nr if_year_admission==1997 | year_admission==2000,  kernel(uni) c(0) p(1) h(1000)
eststo `var'_e1997
regsave RD_Estimate using "${temp}\RD1997_2000.dta", replace ci addlabel(variable, `var')
restore
}

foreach var in  urate1996 avg_loenmv_131996 urban top5 n_conationals s_conationals {
preserve
rdrobust `var' day_nr if_year_admission==1997 | year_admission==2000,  kernel(uni) c(0) p(1) h(1000)
eststo `var'_e1997
regsave RD_Estimate using "${temp}\RD1997_2000.dta", append ci addlabel(variable, `var')
restore
}






*CREATE TABLE WITH BALANCING TESTS FOR INDIVIDUAL CHARACTERISTICS

clear
use  "${temp}\covar.dta"
merge 1:1 variable using "${temp}\RD.dta"
drop N var _merge


format mean sd coef stderr ci_lower ci_upper %3.2fc


tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

gen sortvar=1 if variable=="age"
replace sortvar=2 if variable=="married"

replace sortvar=4 if variable=="female"
replace sortvar=7 if variable=="kids02"
replace sortvar=8 if variable=="kids018"
replace sortvar=9 if variable=="iraq"
replace sortvar=10 if variable=="afghanistan"
replace sortvar=11 if variable=="other_country"
replace sortvar=12 if variable=="arabic"
replace sortvar=13 if variable=="dari"
replace sortvar=15 if variable=="danishf1"
replace sortvar=16 if variable=="danishf2"
replace sortvar=17 if variable=="danishf3"
replace sortvar=22 if variable=="quota"
replace sortvar=23 if variable=="convention"
replace sortvar=24 if variable=="familyref"
replace sortvar=25 if variable=="other_permit"
replace sortvar=26 if variable=="waittime"



replace variable="Age" if variable=="age"
replace variable="Married" if variable=="married"
replace variable="Family-Reunified" if variable=="familyref"
replace variable="Female" if variable=="female"
replace variable="No. Children $<$ 3y" if variable=="kids02"
replace variable="No. Children 3-17y" if variable=="kids018"
replace variable="Iraq" if variable=="iraq"
replace variable="Afghanistan" if variable=="afghanistan"
replace variable="Other Country" if variable=="other_country"
replace variable="Speaks Arabic" if variable=="arabic"
replace variable="Speaks Dari" if variable=="dari"
replace variable="Quota Refugee" if variable=="quota"
replace variable="Convention Refugee" if variable=="convention"
replace variable="Other Refugee" if variable=="other_permit"
replace variable="Asylum Processing Time (Days)" if variable=="waittime"
replace variable="Danish 1" if variable=="danishf1"
replace variable="Danish 2" if variable=="danishf2"
replace variable="Danish 3" if variable=="danishf3"



sort sortvar

preserve
*TABLE WITH BALANCING TESTS FOR INDIVIDUAL CHARACTERISTICS
drop if variable=="Asylum Processing Time (Days)"
listtex variable mean sd  coef ci using "${output}\table1b.tex", rstyle(tabular) replace
restore

preserve
keep if variable=="Asylum Processing Time (Days)"
*TABLE WITH BALANCING TESTS ASYLUM PROCESSING TIME
listtex variable mean sd coef ci using "${output}\table1a_waittime.tex", rstyle(tabular) replace
restore


save "${temp}\Balancing_tests.dta", replace


*CREATE TABLE WITH BALANCING TESTS FOR MUNICIPALITY CHARACTERISTICS
clear
use  "${temp}\covar_mun.dta"
merge 1:1 variable using "${temp}\RD_mun.dta"
keep if _merge==3
drop N var _merge


format mean sd coef stderr ci_lower ci_upper %3.2fc


tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

gen sortvar=1 		if variable=="urban"
replace sortvar=2	if variable=="top5"
replace sortvar=4 	if variable=="emprate1996"
replace sortvar=5 	if variable=="urate1996"
replace sortvar=8 	if variable=="avg_loenmv_131996"
replace sortvar=11 	if variable=="n_conationals"
replace sortvar=12 	if variable=="s_conationals"


replace variable="Employment Rate 1996" if variable=="emprate1996"
replace variable="Unemployment Rate 1996" if variable=="urate1996"
replace variable="Earnings 1996" if variable=="avg_loenmv_131996"
replace variable="Urban Municipality" if variable=="urban"
replace variable="Five Largest Cities" if variable=="top5"
replace variable="Number of Co-Nationals" if variable=="n_conationals"
replace variable="Share of Co-Nationals" if variable=="s_conationals"

sort sortvar



*TABLE WITH BALANCING TESTS FOR MUNICIPALITY CHARACTERISTICS
listtex variable mean sd coef ci using "${output}\table1c.tex", rstyle(tabular) replace








/* CREATE TABLE FOR MUNICIPALITY CHARACTERISTICS 1997 VS. 2000 */
clear
use  "${temp}\covar_mun1997_2000.dta"
merge 1:1 variable using "${temp}\RD1997_2000.dta"
keep if _merge==3
drop N var _merge


format mean sd coef stderr ci_lower ci_upper %3.2fc


tostring ci_lower, gen(ci_l) force format(%3.2f)
tostring ci_upper, gen(ci_u) force format(%3.2f)

gen tt="["
gen ci= tt +ci_l + " ; " + ci_u + "]"
drop tt ci_l ci_u

gen sortvar=1 		if variable=="urban"
replace sortvar=2	if variable=="top5"
replace sortvar=4 	if variable=="emprate1996"
replace sortvar=5 	if variable=="urate1996"
replace sortvar=8 	if variable=="avg_loenmv_131996"
replace sortvar=11 	if variable=="n_conationals"
replace sortvar=12 	if variable=="s_conationals"


replace variable="Employment Rate 1996" if variable=="emprate1996"
replace variable="Unemployment Rate 1996" if variable=="urate1996"
replace variable="Earnings 1996" if variable=="avg_loenmv_131996"
replace variable="Urban Municipality" if variable=="urban"
replace variable="Five Largest Cities" if variable=="top5"
replace variable="Number of Co-Nationals" if variable=="n_conationals"
replace variable="Share of Co-Nationals" if variable=="s_conationals"

sort sortvar


*TABLE COMPARING MUNICIPALITY CHARACTERISTICS 1997 VS. 2000
listtex variable mean sd coef ci using "${output}\oa_table3.tex", rstyle(tabular) replace







erase "${temp}\RD.dta"
erase "${temp}\RD1997_2000.dta"
erase "${temp}\RD_mun.dta"
erase "${temp}\covar.dta"
erase "${temp}\covar_mun.dta"
erase "${temp}\covar_mun1997_2000.dta"
erase "${temp}\Balancing_tests.dta"
